<?php

abstract class db {

    protected $db;
    protected $dsn = 'mysql:dbname=sign_in;host=localhost';
    protected $user = 'root';
    protected $password = '';

    //数据库连接
    public function db() {
        try {
            $this->db = new PDO($this->dsn, $this->user, $this->password, array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));
            $this->db->exec("SET NAMES utf8");
        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
            print_r($this->db->errorInfo());
        }
    }

    function db_con() {
        try {
            $this->db = new PDO($this->dsn, $this->user, $this->password, array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));
            $this->db->exec("SET NAMES utf8");
        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
            print_r($this->db->errorInfo());
        }
    }

    //数据库统计函数
    function db_count($table_name, $rules) {
        $sql = "SELECT count(*) FROM " . $table_name . " WHERE data_status=0 ";
        if (is_string($rules) && $rules != "") {
            $sql = "SELECT count(*) FROM " . $table_name . " WHERE data_status=0 AND " . $rules;
        }
        $s = $this->db->query($sql);
        if ($s) {
            $data = $s->fetchColumn();
        }
        return $data;
    }

    //数据库查询函数
    function db_select($table_name, $rules = '', $key = "*", $limit = '1') {
        $sql = "SELECT " . $key . " FROM " . $table_name . " WHERE data_status=0 ";
        if (is_string($rules) && $rules != "") {
            $sql .=" AND " . $rules;
        }
        $start = ($limit - 1) * 50;
        $sql .= " limit " . $start . ",50";
        $s = $this->db->query($sql);
        if ($data = $s->fetchAll()) {
            return $data;
        } else {
            return "";
        }
    }

    //获取数据行信息
    function GetSigleInfo($table_name, $id) {
        $sql = "SELECT * FROM " . $table_name . " WHERE id = " . $id;
        $s = $this->db->query($sql);
        if ($data = $s->fetch()) {
            return $data;
        } else {
            return "";
        }
    }

}

?>
